package com.prime.tutorial.moviestore.service.reports;

import com.prime.tutorial.moviestore.Constants;
import com.prime.tutorial.moviestore.dao.*;
import com.prime.tutorial.moviestore.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Implementation of {@link ReportsService}}<br/>
 * @author vasylja
 * @author iryna.masyuk
 */
//TODO delete obsolete methods! Clear which reports to drop.
@Service
public class ReportsServiceImpl implements ReportsService {
    private DeviceDAO deviceDAO;
    private ModelDAO modelDAO;
    private ModelTypeDAO modelTypeDAO;
    private StateDAO stateDAO;
    private UserDAO userDAO;

    @Autowired
    public ReportsServiceImpl(DeviceDAO deviceDAO,
                              ModelDAO modelDAO,
                              StateDAO stateDAO,
                              ModelTypeDAO modelTypeDAO,
                              UserDAO userDAO) {
        this.deviceDAO=deviceDAO;
        this.modelDAO = modelDAO;
        this.stateDAO=stateDAO;
        this.modelTypeDAO = modelTypeDAO;
        this.userDAO = userDAO;
    }

   //OK
    public List<Users> getActiveUsersReport() {
        //Users testUser= new Users(new Groups("reports"),
        //                              "rep",
        //                              "rep",
        //                              "rep",
        //                              "rep",
        //true,"Example to test reports",new Date(2009,11,20),new Date(2009,11,24));
        //List<Users> testList=new ArrayList<Users>();
        //testList.add(testUser);
        //return testList;
        return userDAO.findOnlineUsers();

    }

    public List<Users> getActiveUsersAtReport(Date start, Date end) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public List<Users> getUserActivityLastReport() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public List<Users> getUserActivityReport() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    //OK
    public List<Device> getDevicesInstalledReport() {
        return deviceDAO.findByState(stateDAO.findByType(Constants.STATE_ENABLED).get(0));

    }

    public List<Device> getCPEsInstalledReport() {
        //Device testDevice= new Device(new Location(new Country("Ukraine"),
                        //              new City("Kyiv"),
                      //                new Street("Skovorody"),
                    //                  new Building("2",15.23,56.78),
                  //                    new Apartment("215")),
                //                      new State("enabled"),
              //                        new Model("SWITCH", new ModelType("SWITCH")),
            //                          "Test",
          //                            "Device to test the report output.");
        //List<Device> testList=new ArrayList<Device>();
        //testList.add(testDevice);
        //return testList;

        ModelType typeCPE = modelTypeDAO.findByType(Constants.MODELTYPE_CPE).get(0);
        
        //TODO only enabled????
        State en = stateDAO.findByType(Constants.STATE_ENABLED).get(0);
        return deviceDAO.findByModelTypeAndState(typeCPE, en);
    }

    public List<Location> getAddressesWithFreePortsReport() {
            Location testLocation= new Location(new Country("Ukraine"),
                                      new City("Kyiv"),
                                      new Street("Skovorody"),
                                      new Building("2",15.23,56.78),
                                      new Apartment("215"));
        List<Location> testList=new ArrayList<Location>();
        testList.add(testLocation);
        return testList;
    }


    public List<Location> getAddressesWithDevicesReport() {
                Location testLocation= new Location(new Country("Ukraine"),
                                      new City("Kyiv"),
                                      new Street("Jaroslaviv Val"),
                                      new Building("2",15.23,56.78),
                                      new Apartment("215"));
        List<Location> testList=new ArrayList<Location>();
        testList.add(testLocation);
        return testList;
    }

    public List<Device> getDevicesToBeInstalledReport() {
        //TODO only planned_installation ???
        //*ira*: no, also AVAILABLE_INSTALLATION and APPROVED_INSTALLATION
        //maybe smth else, but it is highly unlikely

        //getting states
        List<State> states = new ArrayList();
        State toInstallState= stateDAO.findByType(Constants.STATE_PLANNED_INSTALLATION).get(0);
        State availableState = stateDAO.findByType(Constants.STATE_AVAILABLE_INSTALLATION).get(0);
        State approvedState = stateDAO.findByType(Constants.STATE_APPROVED_INSTALLATION).get(0);

        states.add(toInstallState);
        states.add(availableState);
        states.add(approvedState);

        List<Device> toInstallDevice=deviceDAO.findByState(states);

        return toInstallDevice;
    }

    //OK
    public List<Device> getListDevicesWithErrorsInTheirStatusReport() {
       return deviceDAO.findByState(stateDAO.findByType(Constants.STATE_WITH_ERRORS).get(0));
    }

    public List<Device> getNetworkWorkloadAnalisisReport() {
//            Device testDevice= new Device(new Location(new Country("Ukraine"),
//                                      new City("Kyiv"),
//                                      new Street("Skovorody"),
//                                      new Building("2",15.23,56.78),
//                                      new Apartment("215")),
//                                      new State("enabled"),
//                                      new Model("ROUTER", new ModelType("ROUTER")),
//                                      "Test",
//                                      "Device to test the report output.");
//        List<Device> testList=new ArrayList<Device>();
//        testList.add(testDevice);
//        return testList;
        return deviceDAO.repWorkload();
    }
}
